<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : '{{ urlfor "ErpEntryController.DataGrid"}}'
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData);
			
			$('#search_key').val() ? params.Filter = $('#search_key').val() : '';
			$('#auditing').val()!='' ? params.Auditing = Number($('#auditing').val()) : '';
			$('#typeid').val()!='' ? params.Typeid = $('#typeid').val() : '';
			
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"data" : "Number", "bSortable": false, render:function(data, type, full, meta) {
				return '<a href="/entry/view/'+ full.Id +'">'+ data +'</a>'	
			}}
			,{"bSortable": false, render:function(data, type, full, meta) {
				return '<a href="javascript:;" data-am-popover="{content: \''+ full.Suppliers.Name +'<br />联系人：'+ full.Suppliers.Contacts +'<br />电话：'+ full.Suppliers.Tel +'\', trigger: \'hover focus\'}">'+ full.Suppliers.Name +'</a>';
				
			}}
			,{"data" : "ProNumber", "bSortable": false}
			,{"data" : "Postage", "bSortable": false}
			,{"data" : "Auditing", "bSortable": false,render:function(data, type, full, meta) {
				if(data == 0) {
					return '<span class="am-badge am-badge-warning">待审核</span>';
				}else if(data == 1) {
					return '<span class="am-badge am-badge-success">已通过</span>';
				}else if(data == 2) {
					return '<span class="am-badge">已拒绝</span>';
                }else if(data == 3) {
                    return '<span class="am-badge am-btn-danger">已冲红</span>';
                }else if(data == 4) {
                    return '<span class="am-badge am-badge-success">已入库</span>';
                }
			}}
			,{"data" : "Reconciliation", "bSortable": false,render:function(data, type, full, meta) {
				if(data == 2) {
					return '<a class="am-badge am-badge-warning">未对</a>';
				}else if(data == 1) {
					return '<a class="am-badge am-badge-primary" data-am-popover="{content: \'对帐人：'+ full.ReconciliationUser +'<br />对账时间：'+ full.ReconciliationDate +'\', trigger: \'hover focus\'}">已对</a>';
				}
				return '';
			}}
			,{"data" : "CreatedAt", "bSortable": false, render:function(data, type, full, meta) {
				return full.CreatedBy + ' ' + timetransDate(data);	
			}}
			,{"data" : "UpdatedAt", "bSortable": false, render:function(data, type, full, meta) {
				return full.UpdatedBy + ' ' + timetransDate(data);	
			}}
			,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"10%",
				"render": function ( data, type, full, meta ) { 
					btns = '';
                    if(full.Auditing == 1 || full.Auditing == 4 || full.Auditing == 3) { //未审核状态
                        btns += '&nbsp;<a class="am-btn am-btn-success am-btn-xs" href="/entry/view/' + full.Id + '">查看</a>';
                    }
					if(full.Auditing == 0 || full.Auditing == 2) { //未审核状态
						btns += '&nbsp;<a class="am-btn am-btn-secondary am-btn-xs" href="/entry/auditing/'+ full.Id +'">审核</a>';
					}
					{{if .deleteBtn}}
						if(full.Auditing == 2) { //未审核状态
							btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-danger am-btn-xs" onclick="delEntry('+ full.Id +');return false;">删除</a>';
						}
					{{end}}

					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 30, 50, 100]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		,"drawCallback": function(settings){
			$('[data-am-popover]').popover();
		}   
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});
	
	function delEntry(id) {
		layer.confirm('确定要删除所选记录吗？删除后不可恢复！', function(index){
			$.post('{{ urlfor "ErpEntryController.Delete"}}', {id:id}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	}
	
	function dialogReback() {
		tables.ajax.reload();
	}
	
	$('#insert-entry').click(function(){
		var url = '{{urlfor "ErpEntryController.SelectSuppliers"}}';
		
		layer.open({
            type: 2,
            title: '选择供应商',
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: ['410px', '480px'],
            content: url
        });
		
		return false;	
	});
	
	function SelectSuppliersRollBack(id) {
		layer.closeAll();
		location = '{{ urlfor "ErpEntryController.Edit" }}/'+id;	
	}
</script>